//
//  SetLoginCodeViewController.m
//  ZYZB
//
//  Created by ENERGY on 2018/7/17.
//  Copyright © 2018年 ENERGY. All rights reserved.
//

#import "SetLoginCodeViewController.h"
#import "LoginViewController.h"
@interface SetLoginCodeViewController ()<UITextFieldDelegate>
@property (nonatomic, strong)UITextField * loginCodeTextField;//输入密码
@property (nonatomic, strong)UITextField * againLoginCodeTextField;//再次输入
@end

@implementation SetLoginCodeViewController(networking)
//设置密码
- (void)setLoginCodeData{
    [self showprogressHUD];
    NSString * timeStr = [self currentTimeStr];
    if (self.inviteCodeStr == nil) {
        self.inviteCodeStr = @"";
    }
    //str
    NSDictionary * dic = @{
                           @"t":timeStr,
                           @"userPass":@"abc123",
                           @"pass_again":@"abc123",
                           @"invite_code":@"",
                           @"phone":@"13889126201"
                           };
    //sign
    NSDictionary * signDic = @{
                               @"t":timeStr,
                               @"userPass":@"abc123",
                               @"pass_again":@"abc123",
                               @"invite_code":@"",
                               @"phone":@"13889126201",
                               @"merchantNo":@"80d709a97af1b8ab"
                               };
    SortMD5Code * md5Code = [[SortMD5Code alloc] init];
    NSString * jsonStr = [md5Code getJsonWithStrDic:dic withSignDic:signDic];
    NSData *postData = [jsonStr dataUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:@"http://120.78.157.132:82/index.php/register/index?api=api1002"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    //设置请求方式
    request.HTTPMethod = @"POST";
    //设置请求参数
    request.HTTPBody = postData;
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        [self hiddenProgressHUD];
        if (connectionError) {
            if (connectionError.code == -1001) {
                [self.view makeToast:@"请求网络超时，请稍后再试" duration:1 position:CSToastPositionCenter];
            }
        }else{
            NSDictionary *dictionary =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];//data解析为dictionary
            if (!dictionary) {
                NSLog(@"数据缺失");
                return;
            }
            //        NSString * compareMD5Str = [md5Code encodeStringWithDictionary:dictionary];
            //        NSString * signResult = [dictionary objectForKey:@"sign"];//接到的json串sign字段
            NSDictionary * jsonData = [md5Code getNSDictionaryData:dictionary];//解析为字典数据
            if ([jsonData[@"code"] isEqualToString:@"000000"]) {
                [self.view makeToast:[NSString stringWithFormat:@"%@", jsonData[@"msg"]] duration:1 position:CSToastPositionCenter title:nil image:nil style:nil completion:^(BOOL didTap) {
                    NSLog(@"成功");
                    [self.navigationController popToRootViewControllerAnimated:YES];
                }];
            }else{
                [self.view makeToast:[NSString stringWithFormat:@"%@", jsonData[@"msg"]] duration:1 position:CSToastPositionCenter];
            }
            //        if ([compareMD5Str isEqualToString:signResult]) {
            //            [self.navigationController popToRootViewControllerAnimated:YES];
            //            NSLog(@"成功");
            //        }else{
            //            NSLog(@"失败");
            //        }
        }
    }];
}
@end

@implementation SetLoginCodeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"设置登录密码";
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self setUpViews];
    
    
}
- (void)setUpViews{
    //锁头1图案
    int lockOneImgX = 50/WIDTH_6_SCALE;
    UIImageView * lockOneImg = [[UIImageView alloc] init];
    if (Is_Iphone_X) {
        lockOneImg.frame = CGRectMake(lockOneImgX, 36/WIDTH_6_SCALE + 24, 12/WIDTH_6_SCALE, 20/WIDTH_6_SCALE);
    }else{
        lockOneImg.frame = CGRectMake(lockOneImgX, 36/WIDTH_6_SCALE, 12/WIDTH_6_SCALE, 20/WIDTH_6_SCALE);
    }
    lockOneImg.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:lockOneImg];
    //上面的输入框
    self.loginCodeTextField = [[UITextField alloc] initWithFrame:CGRectMake(lockOneImg.right + 12/WIDTH_6_SCALE, 0, SCREEN_WIDTH - lockOneImg.right - 12/WIDTH_6_SCALE - lockOneImg.left, 22/WIDTH_6_SCALE)];
    self.loginCodeTextField.delegate = self;
    self.loginCodeTextField.centerY = lockOneImg.centerY;
//    self.loginCodeTextField.backgroundColor = [UIColor greenColor];
    self.loginCodeTextField.font = DEF_FontSize_12;
    self.loginCodeTextField.placeholder = @"请输入登录密码";
    self.loginCodeTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    [self.view addSubview:self.loginCodeTextField];
    //上面横线
    UIView * topLineView = [[UIView alloc] initWithFrame:CGRectMake(lockOneImg.left, lockOneImg.bottom + 8/WIDTH_6_SCALE, SCREEN_WIDTH - lockOneImgX*2, 1/WIDTH_6_SCALE)];
    topLineView.backgroundColor = getColor(grayColor);
    [self.view addSubview:topLineView];
    //锁头2图案
    UIImageView * lockTwoImg = [[UIImageView alloc] initWithFrame:CGRectMake(lockOneImg.left, topLineView.bottom + 32/WIDTH_6_SCALE, 16/WIDTH_6_SCALE, lockOneImg.height)];
    lockTwoImg.centerX = lockOneImg.centerX;
    lockTwoImg.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:lockTwoImg];
    //下面输入框
    self.againLoginCodeTextField = [[UITextField alloc] initWithFrame:CGRectMake(self.loginCodeTextField.left, 0, self.loginCodeTextField.width, self.loginCodeTextField.height)];
    self.againLoginCodeTextField.centerY = lockTwoImg.centerY;
    self.againLoginCodeTextField.delegate = self;
//    self.againLoginCodeTextField.backgroundColor = [UIColor greenColor];
    self.againLoginCodeTextField.font = DEF_FontSize_12;
    self.againLoginCodeTextField.placeholder = @"请输入确认密码";
    self.againLoginCodeTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    [self.view addSubview:self.againLoginCodeTextField];
    //下面横线
    UIView * bottomLineView = [[UIView alloc] initWithFrame:CGRectMake(topLineView.left, lockTwoImg.bottom + 8/WIDTH_6_SCALE, topLineView.width, topLineView.height)];
    bottomLineView.backgroundColor = getColor(grayColor);
    [self.view addSubview:bottomLineView];
    //密码说明
    UILabel * introduceLab = [[UILabel alloc] initWithFrame:CGRectMake(bottomLineView.left, bottomLineView.bottom + 17/WIDTH_6_SCALE, bottomLineView.width, 10/WIDTH_6_SCALE)];
    [self label:introduceLab withFont:DEF_FontSize_10 withText:@"密码为6-20位，由数字、字母和符号三种组成" withTextColor:getColor(@"cbcbcb") withTextAlignment:NSTextAlignmentLeft withView:self.view];
    //完成注册
    UIButton * finishiRegisterBtn = [[UIButton alloc] initWithFrame:CGRectMake(bottomLineView.left, introduceLab.bottom + 36/WIDTH_6_SCALE, SCREEN_WIDTH - bottomLineView.left * 2, 37/WIDTH_6_SCALE)];
    [self button:finishiRegisterBtn withBGColor:getColor(@"3675bd") withLayerBorderColor:nil withLayerBorderWith:0 withCornerRadius:6 withMasksToBounds:YES withViwe:self.view];
    [finishiRegisterBtn setTitle:@"完成注册" forState:UIControlStateNormal];
    [finishiRegisterBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    finishiRegisterBtn.titleLabel.font = DEF_FontSize_14;
    [finishiRegisterBtn addTarget:self action:@selector(finishiRegisterBtnClick:) forControlEvents:UIControlEventTouchUpInside];
}
#pragma mark - action
- (void)finishiRegisterBtnClick:(UIButton *)sender{
    NSLog(@"完成注册");
    //网络数据
    [self setLoginCodeData];
}
@end
